﻿Public Class AtaqeDelOwner


    Private cont As Integer = 0
    Private fallos As Integer = 0
    Private x As New Random
    Private palabras As New Palabras
    Private puntuacion As Integer = 0
    Private velocidad As Single = 1
    Private Const elementosANoBorrar = 4
    Private Const finalPantalla = 283
    Private Const spawn = 75
    Private Const anchomaximo = 300
    Private Const alto = 20
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        cont += 1
        Dim lbl As New Label
        Randomize()
        If cont = spawn Then
            lbl.Text = palabras.AleatoryWord
            lbl.Location = New Point(x.Next(anchomaximo), alto)
            'lbl.Enabled = False
            Me.Controls.Add(lbl)
            cont = 0
        End If
        For Each etiqueta In Me.Controls
            If etiqueta.GetType = GetType(Label) Then
                TryCast(etiqueta, Label).Top += velocidad
                If TryCast(etiqueta, Label).Top > finalPantalla Then
                    Me.Controls.Remove(etiqueta)
                    fallos += 1
                    If fallos = 3 Then
                        Timer1.Stop()
                        DialogResult = MessageBox.Show("Has perdido :( tu puntuación ha sido " & lblpuntuacion.Text & vbNewLine & "Aceptar para volver a jugar" & vbNewLine & "Cancelar para ver puntuaciones", "PELAU", MessageBoxButtons.OKCancel)
                        If DialogResult = Windows.Forms.DialogResult.OK Then
                            Main.score.cargar(1)
                            Main.score.comprobar(lblpuntuacion.Text, 1)
                            Main.reiniciar(1)
                        Else
                            Main.score.cargar(1)
                            Main.score.comprobar(lblpuntuacion.Text, 1)
                            Main.cambio(1)
                        End If
                    End If
                End If
            End If
        Next
    End Sub
    Private tamaño As Integer

    Private Sub txtpalabra_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtpalabra.KeyDown
        If e.KeyCode = Keys.Escape Then
            Timer1.Stop()
            Main.cambio(0)
        End If
    End Sub
    Private Sub txtpalabra_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpalabra.KeyPress
        tamaño = txtpalabra.Text.Length
        If e.KeyChar = ChrW(Keys.Return) Then
            For i As Integer = Me.Controls.Count - 1 To elementosANoBorrar Step -1
                Dim lbl As Label = TryCast(Me.Controls.Item(i), Label)
                If txtpalabra.Text.ToLower = lbl.Text.ToLower Then
                    tamaño = 0
                    Me.Controls.Remove(lbl)
                    puntuacion += 1
                    velocidad += 0.05
                    lblpuntuacion.Text = puntuacion
                    txtpalabra.Text = ""
                Else
                    If i = elementosANoBorrar Then
                        tamaño = 0
                        txtpalabra.Text = ""
                    End If
                End If
            Next
        End If
    End Sub

   

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Timer1.Start()
        cont = 9
    End Sub

    Private Sub txtpalabra_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpalabra.TextChanged
        If txtpalabra.Text.Length - tamaño >= 2 Then
            txtpalabra.Text = ""
        End If
    End Sub
End Class

